table.insert(Assets, Asset("ANIM", "anim/rg_action_dodge.zip"))
-- roll[down]  30/30.0
-- roll[side]  30/30.0
-- roll[up]    35/30.0
-- slide_pre[down] 5/30.0
-- slide_loop[down]    5/30.0
-- slide_pst[down] 11/30.0
-- slide_pre[side] 5/30.0
-- slide_loop[side]    5/30.0
-- slide_pst[side] 11/30.0
-- slide_pre[up]   5/30.0
-- slide_loop[up]  5/30.0
-- slide_pst[up]   11/30.0


local dodge = Action({},-5, nil, nil, 2, nil, true)
dodge.distance = math.huge
dodge.id = "RG_DODGE"
dodge.str = STRINGS.ACTIONS.RG_DODGE

AddStategraphState("wilson", State{
    name = "rg_dodge",
    tags = {"busy", "dodge","no_stun","canrotate"},

    onenter = function(inst)
        -- dumptable(inst:GetBufferedAction(),1,1,1)
        local action = inst:GetBufferedAction()
        if action and action.pos then
            inst:ForceFacePoint(action.pos)
        end

        inst.sg:SetTimeout(inst.components.rg_playerskill:GetDodgeTimeout())
        inst.components.rg_playerskill:OnDodge()

        inst.components.locomotor:Stop()
        inst.AnimState:PlayAnimation("slide_pre")
        inst.AnimState:PushAnimation("slide_loop")
        inst.SoundEmitter:PlaySound("dontstarve_DLC003/characters/wheeler/slide")
        inst.Physics:SetMotorVelOverride(20,0,0)
        inst.components.locomotor:EnableGroundSpeedMultiplier(false)
                
    end,

    ontimeout = function(inst)
        inst.sg:GoToState("rg_dodge_pst")
    end,

    onupdate = function(inst)
        inst.Physics:SetMotorVelOverride(20,0,0) --?
    end,

    onexit = function(inst)
        inst.components.locomotor:EnableGroundSpeedMultiplier(true)
        inst.Physics:ClearMotorVelOverride()
        inst.components.locomotor:Stop()
        
        inst:ClearBufferedAction()
        inst.components.locomotor:SetBufferedAction(nil)
    end,
})

AddStategraphState("wilson", State{
    name = "rg_dodge_pst",
    tags = {"dodge","no_stun"},

    onenter = function(inst)
        inst.AnimState:PlayAnimation("slide_pst")
    end,

    events =
    {
        EventHandler("animover", function(inst)
            inst.sg:GoToState("idle")
        end ),
    }
})


local function IsPointValid(pt)
    local ground = GetWorld()
    if ground then
        local tile = ground.Map:GetTileAtPoint(pt.x, pt.y, pt.z)
        return tile ~= GROUND.IMPASSABLE and tile < GROUND.UNDERGROUND
    end
end

-----------

RG_AddComponentPostInit("playeractionpicker", function(self)
    local inst = self.inst

    local old_get = self.DoGetMouseActions
    function self:DoGetMouseActions(...)
        local left, right = old_get(self, ...)

        if TheInput:GetHUDEntityUnderMouse() ~= nil then
            return left, right
        end

        local pos = TheInput:GetWorldPosition()

        if inst.components.rg_playerskill:CanDodge() and IsPointValid(pos) and not inst.sg:HasStateTag("dodge") then
            if right == nil then
                right = BufferedAction(self.inst, nil, dodge, nil, pos)
            else
                if right.action and (right.action.priority < -1 and right.action.id ~= 'LIGHT')then
                    right = BufferedAction(self.inst, nil, dodge, nil, pos)
                end
            end
        end
        return left, right
    end
end)

AddStategraphActionHandler("wilson", ActionHandler(dodge, "rg_dodge"))
